$(function () {
    let layer = layui.layer;
    
    init();

    $('.logout').on('click' ,function(){
        layer.confirm('您确定退出吗?', {icon: 3, title:'提示'}, function(index){
            //退出就是清空token
            localStorage.removeItem('BigDashijianToken');
            location.href = '/login.html';
            layer.close(index);
          });
    })
})

//请求函数  将其放在入口函数外部，暴露在全局，挂载在window下，这样才能使其他ifram页面调用
function init() {
    $.ajax({
        method: 'get',
        url: '/my/userinfo',
        //  /my开头的请求 要添加token
        // headers: {
        //     Authorization: localStorage.getItem('BigDashijianToken') || ''
        // },
        success(res) {
            if (res.status === 0) {
                renderAvatar(res);
            } else {
                layer.msg(res.message, { icon: 2 });
            }
        },
        
    });
}
function renderAvatar(res){
    let name =  (res.data.nickname || res.data.username).replace(/\s/ig , '');
    $('.welcome').html('欢迎你 ' + name);
    $('.name-img').text(name[0].toUpperCase());
    if( res.data.user_pic){
        $('.layui-nav-img').prop('src' , res.data.user_pic);
        $('.layui-nav-img').show();
        $('.name-img').hide()
    }else{
        $('.layui-nav-img').hide()
        $('.name-img').css('display', 'inline-block')
    }
}